<?php
/**
 * Description
 * User: duanwenjie
 * Date: 2019/5/9
 * Time: 11:30 AM
 */

namespace app\index\controller;

use app\common\ConfigBase;
use app\common\model\ToolModel;
use app\common\YujisoController;
use app\index\service\ShopService;
use think\Exception;

class Shop extends YujisoController
{
    public $param;
    public function __construct()
    {
        parent::__construct();
        $temp = $this->request->param();
        if (isset($temp['s'])) unset($temp['s']);
        $this->param = $temp;
    }

    /**
     * 获取店铺列表
     * @author duanwenjie
     */
    public function getShopList()
    {
        $param = $this->param;
        $result = ShopService::getShopList($param);
        returnJson('1','查询成功',$result['list'],$result['total']);
    }


    /**
     * 启用/禁用
     * @author duanwenjie
     */
    public function disableOrEnable()
    {
        $param =$this->param;
        $rules = [
            'id' => 'require',
        ];
        $this->validateData($rules);
        ToolModel::updateData(['id' => $param['id']], ['status' => $param['status']],'base_shop');
        returnJson(1,'操作成功');
    }


    /**
     * 新增/编辑
     * @author duanwenjie
     */
    public function edit()
    {
        try{
            $param = $this->param;
            $id = "";
            if ($this->request->isPost()){
                /*编辑*/
                if (isset($param['id']) && !empty($param['id'])){
                    $id = $param['id'];
                    unset($param['id']);
                }
                $res = ShopService::checkData($param, $id);
                $param['base_company_id'] = $res['base_company_id']; // 公司ID
                $param['exchange_rate_id'] = $res['exchange_rate_id']; // 结算货币ID
                $param['base_country_id'] = $res['base_country_id']; // 国家ID
                $param['base_country_name'] = $res['base_country_name']; // 国家名称
                unset($param['company_name']);
                if (!empty($id)){
                    ShopService::updateWarehouse($id,$param['base_country_id'],$param['base_country_name'],$param['base_company_id']);
                    ToolModel::updateData(['id' => $id], $param, 'base_shop');
                }else{
                    ToolModel::insertData($param, 'base_shop');
                }
                returnJson(1, "操作成功");
            }else{
                $result = ShopService::getShopList($param);
                returnJson(1, '', $result['list']);
            }
        }catch (Exception $e){
            trace($e->getMessage(), 'error');
            returnJson(500, '失败:'.$e->getMessage());
        }
    }

    /**
     * 获取币种信息
     * @author duanwenjie
     */
    public function getCurrency()
    {
        exitJson(ConfigBase::getCurrencyAllInfo());
    }

    /**
     * 获取公司信息(已启用)
     * @author duanwenjie
     */
    public function getCompany()
    {
        exitJson(ConfigBase::getCompany());
    }

    /**
     * 获取SAP国家店铺code码相关数据信息
     * @author duanwenjie
     */
    public function getSapCodeInfo()
    {
        $res = curl('http://10.90.1.174:801/yujisopms/sap.php?type=countryShop', []);
        if (!empty($res)){
            $info = json_decode($res, true);
            if (isset($info['data']) && !empty($info['data'])){
                returnJson('1', '获取成功', $info['data']);
            }else{
                returnJson('200', 'SAP国家店铺code码为空');
            }
        }else{
            returnJson('200', 'SAP国家店铺code码获取失败');
        }

    }
}